/*
 * Copyright 2018 Youchaoshi authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package common.base.operationHelper;

import android.content.Context;
import android.os.Environment;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okgo.request.base.Request;
import com.lzy.okserver.OkDownload;
import com.lzy.okserver.download.DownloadListener;
import com.lzy.okserver.download.DownloadTask;

import java.io.File;

/**
 * OkGo下载辅助类
 * Created by JustTong on 2018/12/11.
 */

public class OkGoDownloadHelper {


    public static OkDownload okDownload = OkDownload.getInstance();
    public static String path = Environment.getExternalStorageDirectory().getPath() + "/XQB/Download/";
    public static DownloadTask task;

    /**
     * OkGo下载
     *
     * @param Url
     */
    public static void DownFile(String Url, DownloadListener listener) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        okDownload.setFolder(path);
        PostRequest request = OkGo.post(Url);
        task = okDownload.request("下载文件", request)
                .save()
                .fileName(Url.substring(Url.lastIndexOf("/") + 1, Url.length()))
                .register(listener);
        task.start();

    }

    public static void DownFile(String Url, String path, DownloadListener listener) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        okDownload.setFolder(path);
        PostRequest request = OkGo.post(Url);
        task = okDownload.request("下载文件", request)
                .save()
                .fileName(Url.substring(Url.lastIndexOf("/") + 1, Url.length()))
                .register(listener);
        task.start();

    }

    public static void down(Context context, String url, FileCallback callback) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }

        OkGo.<File>get(url)
                .tag(context)
                .execute(callback);

    }


    private static String mBasePath; //本地文件存储的完整路径  /storage/emulated/0/book/恰似寒光遇骄阳.txt

    /**
     * @param context          上下文
     * @param fileUrl          下载完整url
     * @param destFileDir      SD路径
     * @param destFileName     文件名
     * @param mFileRelativeUrl 下载相对地址
     *                         （我们从服务器端获取到的数据都是相对的地址）例如： "filepath": "/movie/20180511/1526028508.txt"
     */
    public static void downloadFile(Context context, String fileUrl, String destFileDir, String destFileName, String mFileRelativeUrl) {
        String mDestFileName = destFileName + mFileRelativeUrl.substring(mFileRelativeUrl.lastIndexOf("."), mFileRelativeUrl.length());
        OkGo.<File>get(fileUrl).tag(context).execute(new FileCallback(destFileDir, mDestFileName) { //文件下载时指定下载的路径以及下载的文件的名称
            @Override
            public void onStart(Request<File, ? extends Request> request) {
                super.onStart(request);
//                LogUtils.e("开始下载文件"+"DDDDD");
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<File> response) {
//                LogUtils.e("下载文件成功"+"DDDDD"+response.body().length());
                mBasePath = response.body().getAbsolutePath();
            }

            @Override
            public void onFinish() {
                super.onFinish();
//                LogUtils.e("下载文件完成"+"DDDDD");
//                SPUtils.getInstance().put("localPath",mBasePath);
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<File> response) {
                super.onError(response);
//                LogUtils.e("下载文件出错"+"DDDDD"+response.message());

            }

            @Override
            public void downloadProgress(Progress progress) {
                super.downloadProgress(progress);
                float dLProgress = progress.fraction;
//                LogUtils.e("文件下载的进度"+"DDDDD"+dLProgress);
            }
        });
    }

    //拼接一个本地的完整的url 供下载文件时传入一个本地的路径
    private static final String mSDPath = Environment.getExternalStorageDirectory().getPath();
    //分类别路径
    private static String mClassifyPath = null;

    public static String customLocalStoragePath(String differentName) {
        File basePath = new File(mSDPath); // /storage/emulated/0
        mClassifyPath = mSDPath + "/" + differentName + "/";  //如果传来的是 book 拼接就是 /storage/emulated/0/book/
        //如果传来的是game  那拼接就是 /storage/emulated/0/game/
        if (!basePath.exists()) {
            basePath.mkdirs();
            System.out.println("文件夹创建成功");
        }

        return mClassifyPath;
    }


    //截取一个文件加载显示时传入的一个本地完整路径
    public static String subFileFullName(String fileName, String fileUrl) {
        String cutName = fileName + fileUrl.substring(fileUrl.lastIndexOf("."), fileUrl.length());  //这里获取的是  恰似寒光遇骄阳.txt
        return cutName;
    }

}
